define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Ads, $stateParams, GoDialog, Auth, GoCatTypeAuth) {

        $scope.act_id = $stateParams.act_id;
        $scope.scope = new Object();
        $scope.scope.name = "";
        $scope.scope.start_time = "";
        $scope.scope.end_time = "";
        $scope.scope.brand_id = "";
        $scope.scope.cat_type = new Array(); //分类数组
        $scope.scope.goods = new Array(); //选择商品数组
        $scope.num = 0; //通过其来控制数组的长度，即下标
        $scope.goodsName = new Array(); //显示用,不用来提交
        $scope.goods_id = 0;
        $scope.scope.goods_select = {id:0,name:"请选择商品"};
        $scope.goodsList = new Array();
        //添加
        $scope.add = function() { //数组长度加1
                if(!$scope.scope.goods_id) {
                    GoDialog.toast("请选择商品！");
                } else {
                    console.log($scope.scope.goods_id);
                    console.log($scope.scope.goods);
                    if($scope.select_goods($scope.scope.goods_id,$scope.scope.goods)){
                        GoDialog.toast("不能选择重复商品！");
                        return;
                    }
                    for(var i = 0; i < $scope.goodsList.length; i++) {
                        if($scope.scope.goods_id == $scope.goodsList[i].goods_id) {
                            // $scope.goodsName[$scope.num] = $scope.goodsList[i].name;
                            $scope.scope.goods.push($scope.goodsList[i]);
                        }
                    }
                }
            }
            //减少
        $scope.reduce = function(index) {
            $scope.scope.goods.splice(index, 1); //一次删除一个
            $scope.goodsName.splice(index, 1); //该数组也要删除一个，要不然显示出来的数据是错误的
            if($scope.num > 0) {
                $scope.num--; //下标相应的要减1
            }
        }

        //判断商品是否重复

        $scope.select_goods=function(id,object){
            for(var k in object){
                if(object[k].goods_id==id){
                    return true;
                }
            }
            return false;
        }

        //监听：显示配件名称 ，而不是id，如果id相同，则将对象的name存放到suitName数组中
        /*$scope.$watch("scope.goods[num]", function(n, o) {
        	for(var i = 0; i < $scope.goodsList.length; i++) {
        		if(n == $scope.goodsList[i].goods_id) {
        			$scope.goodsName[$scope.num] = $scope.goodsList[i].name;
        		}
        	}
        })*/

        $scope.goodsList = new Array();
        $scope.init_data = function() {
            if($scope.act_id) { //判断是否在原有内容的基础上进行编辑
                Ads.detail($scope.act_id, function(data) {
                    $scope.detail = data;
                    $scope.scope.name = $scope.detail.name;
                    $scope.scope.start_time = $scope.detail.start_time;
                    $scope.scope.end_time = $scope.detail.end_time;
                    $scope.scope.brand_id = $scope.detail.brand_id;
                    $scope.scope.cat_type = $scope.detail.cat_type; //分类数组
                    $scope.scope.goods = $scope.detail.goods; //选择商品数组
                });
            }
            Ads.brandList(function(data) {
                $scope.brandList = data.brand;
            });
            Ads.goodsList(7, function(data) {
                $scope.goodsList = data.data;
            });
        }
        $scope.init_data();

        /***分类三级联动***/
        $scope.input = new Object();
        $scope.input.store_id = Auth.get_store_id(); //隐藏的企业id的绑定数据
        $scope.input.brand = {id:0,name:"请选择品牌"};; //这是品牌下拉框的绑定数据
		$scope.input.cat1 = {id:0,name:"请选择大分类"};
		$scope.input.cat2 = {id:0,name:"请选择中分类"};
		$scope.input.cat3 = {id:0,name:"请选择小分类"};
        //下拉侦听
        $scope.listen_brand = function(data, obj) {
            $scope.input.cat1 = {id:0,name:"请选择大分类"};
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        $scope.listen_cat1 = function(data, obj) {
            $scope.input.cat2 = {id:0,name:"请选择中分类"};
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        $scope.listen_cat2 = function(data, obj) {
            $scope.input.cat3 = {id:0,name:"请选择小分类"};
        };
        //定义绑定
        var items_cat = new Array();
        var obj_brand = {
            name: 'input.brand'
        };
        var obj1_cat = {
            name: 'input.cat1'
        };
        var obj2_cat = {
            name: 'input.cat2'
        };
        var obj3_cat = {
            name: 'input.cat3'
        };
        items_cat.push(obj_brand);
        items_cat.push(obj1_cat);
        items_cat.push(obj2_cat);
        items_cat.push(obj3_cat);
        GoCatTypeAuth.init_load($scope, items_cat);
        $scope.doSelectBrand = function(){
			//id是值 name是显示
			GoDialog.showSelect("品牌",$scope.brand_items,$scope.input.brand,function(data){
				$scope.input.brand = data;
			});
		}
        $scope.doSelectCat1 = function(){
			//id是值 name是显示
			GoDialog.showSelect("大分类",$scope.cat1_items,$scope.input.cat1,function(data){
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function(){
			//id是值 name是显示
			GoDialog.showSelect("中分类",$scope.cat2_items,$scope.input.cat2,function(data){
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function(){
			//id是值 name是显示
			GoDialog.showSelect("小分类",$scope.cat3_items,$scope.input.cat3,function(data){
				$scope.input.cat3 = data;
			});
        }
        $scope.doSelectGoods = function(){
            //id是值 name是显示
            var list = new Array();
            $scope.goodsList.forEach(obj => {
                if(obj.id == undefined)
                obj.id = obj.goods_id;
                if(obj.name == undefined)
                obj.name = obj.goods_name;
            });
			GoDialog.showSelect("商品",$scope.goodsList,$scope.scope.goods_select,function(data){
                $scope.scope.goods_id = data.goods_id;
                $scope.scope.goods_select = data;
			});
		}
        $scope.ads = function() {
                console.log($scope.scope.start_time);
                if(!$scope.scope.name) {
                    GoDialog.toast("请输入活动名称！");
                } else if(!$scope.scope.start_time) {
                    GoDialog.toast("请输入活动开始时间！");
                } else if(!$scope.scope.end_time) {
                    GoDialog.toast("请输入活动结束时间！");
                } else if(!$scope.scope.brand_id) {
                    GoDialog.toast("请选择品牌！");
                } else if($scope.scope.cat_type.length == 0) {
                    GoDialog.toast("请选择分类！");
                } else if($scope.scope.goods.length == 0) {
                    GoDialog.toast("请选择商品！");
                } else {
                    Ads.edit($scope.act_id, $scope.scope.name, $scope.scope.start_time, $scope.scope.end_time, $scope.scope.brand_id, $scope.scope.cat_type, $scope.scope.goods, function(data) {
                        if(data.success)
                            GoDialog.toast("保存数据成功！");
                        else
                            GoDialog.toast("保存数据失败！");
                    });
                }
            }
            //取消
        $scope.tui = function() {
            window.history.go(-1);
        }

        //显示分类
        $scope.act_type = new Array();
        var index = 0;
        $scope.add_act_type = function() {
            var id="";
            var name ="";
            if(!$scope.input.brand || $scope.input.brand.id ==0){
                 GoDialog.toast("请选择品牌！");
                 return;
            }
            if(!$scope.input.cat1 || $scope.input.cat1.id ==0) {
                GoDialog.toast("请选择分类！");
                return;
            } else if(!$scope.input.cat2 || $scope.input.cat2.id ==0) {
                id = $scope.input.cat1.id;
                name = $scope.input.cat1.name;

            } else if(!$scope.input.cat3 || $scope.input.cat3.id ==0) {
                id = $scope.input.cat1.id + "," + $scope.input.cat2.id;
                name = $scope.input.cat1.name + "/" + $scope.input.cat2.name;
            } else {
                id = $scope.input.cat1.id + "," + $scope.input.cat2.id + "," + $scope.input.cat3.id;
                name = $scope.input.cat1.name + "/" + $scope.input.cat2.name + "/" + $scope.input.cat3.name;
            }
            if($scope.select_type(name, $scope.act_type)){
                GoDialog.toast("不能选择相同的分类！");
            }else{
                $scope.act_type[index] = {
                    "id": id,
                    "name": name
                }
                index++;
            }

        }

        //判断 是否重复
        $scope.select_type = function(name, object) {
                for(var k in object) {
                    if(object[k].name == name) {
                        return true;
                    }
                }
                return false;
            }
            //删除分类
        $scope.del = function(id) {
            $scope.act_type.splice(id,1);
            index--;
        }

    }

    controller.$inject = ['$scope', 'Ads', '$stateParams', 'GoDialog', 'Auth', 'GoCatTypeAuth'];

    return app.registerController("adsctrl",controller);
});